package cn.com.qing.mydb.dbhelp;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.alibaba.fastjson.JSON;

import cn.com.qing.mydb.dbhelp.help.DBConnection;
import cn.com.qing.mydb.dbhelp.reflect.MapReflect;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
    	for(int j =0;j<100;j++)
    	try{
    		System.out.println( "Hello World!" );
            Properties properties = MyResource.getResourceAsProperties("default-config.properties");
    		Connection connection = DBConnection.getConnect(properties);
    		Statement s = connection.createStatement();
    		long timestart = System.currentTimeMillis();
    		String sql = "SELECT * FROM user WHERE 1=1";
    		ResultSet rs = s.executeQuery(sql);
    		ResultSetMetaData rsmd = rs.getMetaData();
    		int rows = rsmd.getColumnCount();
    		List<Map<String, Object>> rsMap = new ArrayList<Map<String, Object>>();
    		while (rs.next()) {
    			Map<String, Object> fieldValue = new HashMap<String, Object>();
    			for (int i = 1; i <= rows; i++) {
    				String fieldClassName = rsmd.getColumnClassName(i);
    				String fieldName = rsmd.getColumnName(i);
    				MapReflect.encasementMapping(fieldClassName, fieldName, rs, fieldValue);
    			}
    			rsMap.add(fieldValue);
    		}

    		rs.close();
    		s.close();
    		connection.close();
    		System.out.println(System.currentTimeMillis()-timestart);
    		System.out.println(JSON.toJSONString(rsMap));
    	}catch (Exception e) {
			e.printStackTrace();
		}
    }
}
